package com.sojson.project.tool.gen.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.project.tool.gen.entity.dto.GenTableColumnDto;
import com.sojson.project.tool.gen.entity.dto.GenTableColumnPageDto;
import com.sojson.project.tool.gen.entity.po.GenTableColumn;
import com.sojson.project.tool.gen.entity.vo.GenTableColumnVo;
import com.sojson.project.tool.gen.service.GenTableColumnService;
import com.sojson.result.Result;
import com.sojson.util.poi.ExcelUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 代码生成业务表字段控制器
 * 
 * @author liu
 * @date 2022-10-28
 */
@Api(tags = "代码生成业务表字段管理")
@RestController
@RequestMapping("/tool/gen/column")
public class GenTableColumnController {

    @Autowired
    private GenTableColumnService genTableColumnService;

    /**
     * 单条插入
     * 
     * @param dto
     * @return
     */
    @PostMapping("/save")
    @PreAuthorize("@ss.hasPermi('/tool/gen/column/save')")
    @ApiOperation("给代码生成业务表字段添加一条数据")
    public Result<Boolean> save(@RequestBody GenTableColumn dto) {
        return Result.success(genTableColumnService.save(dto));
    }

    /**
     * 列表查询
     * 
     * @param dto
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('/tool/gen/column/list')")
    @ApiOperation(value = "查询代码生成业务表字段数据", response = GenTableColumnVo.class)
    public Result<List<GenTableColumnVo>> list(GenTableColumnDto dto) {
        return Result.success(genTableColumnService.list(dto));
    }

    /**
     * 分页查询
     * 
     * @param dto
     * @return
     */
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermi('/tool/gen/column/page')")
    @ApiOperation(value = "分页查询代码生成业务表字段数据", response = GenTableColumnVo.class)
    public Result<List<GenTableColumnVo>> page(GenTableColumnPageDto dto) {
        return Result.success(genTableColumnService.page(dto));
    }

    /**
     * 导出数据
     * 
     * @param dto
     * @return
     * @throws Exception 
     */
    @GetMapping("/export")
    @PreAuthorize("@ss.hasPermi('/tool/gen/column/export')")
    @ApiOperation("导出代码生成业务表字段数据")
    public void export(GenTableColumnDto dto) throws Exception {
        List<GenTableColumnVo> list = genTableColumnService.list(dto);
        ExcelUtil<GenTableColumnVo> excelUtil = new ExcelUtil<>(GenTableColumnVo.class);
        excelUtil.exportExcelOutFile(list, "代码生成业务表字段");
    }

    /**
     * 清空数据
     * 
     * @return
     */
    @DeleteMapping("/clear")
    @PreAuthorize("@ss.hasPermi('/tool/gen/column/clear')")
    @ApiOperation("清空代码生成业务表字段数据")
    public Result<Boolean> clear() {
        return Result.success(genTableColumnService.clear());
    }

    /**
     * 通过iD删除
     * 
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("@ss.hasPermi('/tool/gen/column/remove/{id}')")
    @ApiOperation("通过iD删除代码生成业务表字段数据")
    public Result<Boolean> remove(@PathVariable String id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(genTableColumnService.removeById(id));
    }

    /**
     * 通过id列表删除
     * 
     * @param dto
     * @return
     */
    @DeleteMapping("/remove")
    @PreAuthorize("@ss.hasPermi('/tool/gen/column/remove')")
    @ApiOperation("通过iD列表删除代码生成业务表字段数据")
    public Result<Boolean> remove(@RequestBody GenTableColumnDto dto) {
        List<String> ids = dto.getIds();
        if (ids == null || ids.size() == 0) {
            return Result.success();
        }

        return Result.success(genTableColumnService.removeByIds(ids));
    }

    /**
     * 通过iD更新
     * 
     * @param dto
     * @return
     */
    @PutMapping("/updateById")
    @PreAuthorize("@ss.hasPermi('/tool/gen/column/updateById')")
    @ApiOperation("通过iD更新代码生成业务表字段数据")
    public Result<Boolean> updateById(@RequestBody GenTableColumn dto) {
        return Result.success(genTableColumnService.updateById(dto));
    }

    /**
     * 通过iD查询
     * 
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("@ss.hasPermi('/tool/gen/column/info/{id}')")
    @ApiOperation(value = "通过iD查询代码生成业务表字段数据", response = GenTableColumnVo.class)
    public Result<GenTableColumnVo> info(@PathVariable String id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(genTableColumnService.findById(id));
    }

}